package com.one.blocks.rbac.controller;

import com.one.blocks.common.request.PageRequest;
import com.one.blocks.common.response.WebPageResponse;
import com.one.blocks.common.response.WebResponse;
import com.one.blocks.mvc.controller.BaseController;
import com.one.blocks.mvc.valid.constraints.NoAllocatedData;
import com.one.blocks.rbac.controller.query.SysUserQuery;
import com.one.blocks.rbac.controller.vo.SysUserVo;
import com.one.blocks.rbac.convert.SysUserConvert;
import com.one.blocks.rbac.domain.SysUser;
import com.one.blocks.rbac.log.Log;
import com.one.blocks.rbac.manager.SysUserManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-02-19 11:14:03
 */
@Api(tags = "系统用户接口")
@RestController
@RequestMapping("/sys/user")
@Slf4j
@Validated
@Log(module = "用户")
public class SysUserController extends BaseController<SysUserManager, SysUserConvert, SysUser, SysUserVo, SysUserQuery> {

    @Autowired
    public SysUserController(SysUserManager manager) {
        super(manager, SysUserConvert.INSTANCE);
    }

    @ApiOperation("查询单个用户")
    @PreAuthorize("sys:user:view")
    @GetMapping
    public WebResponse<SysUserVo> get(@RequestParam Integer id) {
        return super.generalGet(id);
    }

    @ApiOperation("根据id批量查询用户")
    @PreAuthorize("sys:user:view")
    @GetMapping("/batch")
    public WebResponse<List<SysUserVo>> getIds(@Validated @NotNull @Size(min = 1, max = 100) @RequestParam(name = "ids") Set<Integer> ids) {
        return super.generalGetIds(ids);
    }

    @ApiOperation("分页查询用户")
    @PreAuthorize("sys:user:view")
    @GetMapping("/page")
    public WebPageResponse<SysUserVo> page(@Validated PageRequest request, @Validated SysUserQuery query) {
        return super.generalPage(request, query);
    }

    @ApiOperation("添加用户")
    @PreAuthorize("sys:user:insert")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Serializable> insert(@Validated @RequestBody SysUserVo sysUserVo) {
        return super.generalInsert(sysUserVo);
    }

    @ApiOperation("更新用户(更新所有字段)")
    @PreAuthorize("sys:user:update")
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Void> update(@Validated @RequestBody SysUserVo sysUserVo) {
        return super.generalUpdate(sysUserVo);
    }

    @ApiOperation("更新用户(只更新非空字段)")
    @PreAuthorize("sys:user:update")
    @PatchMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Void> updateSelective(@Validated @RequestBody SysUserVo sysUserVo) {
        return super.generalUpdateSelective(sysUserVo);
    }

    @ApiOperation("删除用户")
    @PreAuthorize("sys:user:delete")
    @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Void> delete(
            @Validated @NotNull @Size(min = 1, max = 100)
            @NoAllocatedData(manager = SysUserManager.class) @RequestBody Set<Integer> ids) {
        return super.generalDeleteByIds(ids);
    }
}